package com.sos.core.game 
{
	import com.sos.core.DataObject;
	import com.sos.logging.Logger;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author Francesco Forte
	 */
	
	
	 
	public class GameProcHolder extends DataObject
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// Property names
		public static const		JSON_PROPERTY_SOT:String				= "sot";
		public static const		JSON_PROPERTY_SCORE:String				= "score";
		public static const		JSON_PROPERTY_EOT:String				= "eot";		
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _procList:Dictionary;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function GameProcHolder(data:Object = null)
		{
			super();
			// Init objects
			_procList = new Dictionary();
			_procList[JSON_PROPERTY_SOT] = new Vector.<GamePlayerProc>;
			
			_procList[JSON_PROPERTY_EOT] = new Vector.<GamePlayerProc>;
			// clears data
			clear();
			// If data is provided, initializes the instance
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------	
		
		public function get procList():Dictionary 
		{
			return _procList;
		}
		
		public function set procList(value:Dictionary):void 
		{
			_procList = value;
		}
		
			
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		override protected function validate(data:Object):void
		{
			// Base properties
			if (!data.hasOwnProperty(JSON_PROPERTY_SOT)) 			throwLoadMissingPropError(JSON_PROPERTY_SOT);
			
		}
		
		override protected function fill(data:Object):void
		{			
			// Temporary effects list
			var procs:Vector.<GamePlayerProc> = new Vector.<GamePlayerProc>;
			var proc:Object;
			// Check if JSON_PROPERTY_SOT exists 
			if (data.hasOwnProperty(JSON_PROPERTY_SOT))
			{
				// Loop over raw effects
				for each (proc in data[JSON_PROPERTY_SOT])
				{
					procs.push(new GamePlayerProc(proc));
				}
			}
			// Clear and popolate effects list
			_procList[JSON_PROPERTY_SOT].splice(0, _procList[JSON_PROPERTY_SOT].length);
			for (var i:int = 0; i < procs.length; i++)
			{
				if (procs[i].procs.length > 0)
					_procList[JSON_PROPERTY_SOT].push(procs[i]);
			}
			
			procs.splice(0, procs.length);
			// Check if JSON_PROPERTY_EOT exists 
			if (data.hasOwnProperty(JSON_PROPERTY_EOT))
			{
				// Loop over raw effects
				for each (proc in data[JSON_PROPERTY_EOT])
				{
					procs.push(new GamePlayerProc(proc));
				}
			}
			// Clear and popolate effects list
			_procList[JSON_PROPERTY_EOT].splice(0, _procList[JSON_PROPERTY_EOT].length);
			for (i = 0; i < procs.length; i++)
			{
				if (procs[i].procs.length > 0)
					_procList[JSON_PROPERTY_EOT].push(procs[i]);
			}
			
			procs.splice(0, procs.length);
			// Check if JSON_PROPERTY_SCORE exists 
			if (data.hasOwnProperty(JSON_PROPERTY_SCORE))
			{
				_procList[JSON_PROPERTY_SCORE] = new Vector.<GamePlayerProc>;
				// Loop over raw effects
				for each (proc in data[JSON_PROPERTY_SCORE])
				{
					procs.push(new GamePlayerProc(proc));
				}
				// Clear and popolate effects list
				_procList[JSON_PROPERTY_SCORE].splice(0, _procList[JSON_PROPERTY_SCORE].length);
				for (i = 0; i < procs.length; i++)
				{
					if (procs[i].procs.length > 0)
						_procList[JSON_PROPERTY_SCORE].push(procs[i]);
				}
			}
			
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		override public function clear():void
		{
			// Clear objects
			_procList[JSON_PROPERTY_SOT].splice(0, _procList[JSON_PROPERTY_SOT].length);
			_procList[JSON_PROPERTY_EOT].splice(0, _procList[JSON_PROPERTY_EOT].length);
			if (_procList[JSON_PROPERTY_SCORE])
				_procList[JSON_PROPERTY_SCORE].splice(0, _procList[JSON_PROPERTY_SCORE].length);
		}
		
		public function getPhaseProcsByPlayer(playerId:uint, phase:String):GamePlayerProc
		{
			for each (var gpc:GamePlayerProc in _procList[phase]) 
			{
				if ((gpc.playerId == playerId)&&(gpc.procs.length > 0))
					return gpc;
			}
			return null;
		}
		
		
	}

}